package com.jala.base.data.redis;


import com.jala.base.data.WxSessionCache;
import com.jala.base.data.WxSessionMap;
import com.jala.ecs.client.cache.SerializeUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class WxRedisSessionCache implements WxSessionCache {

	private static final Log log = LogFactory.getLog(WxRedisSessionCache.class);

	private JedisPool jedisPool = null;
	
	private int expire = 7200;
	
	/**
	 * The Redis key prefix for the sessions 
	 */
	private String keyPrefix = "wx_sessionid:";
	/**
	 * The Redis key prefix for the sessions 
	 */
	private String ticketKeyPrefix = "ecsclient_sessionid:cas_ticket:";
	

	public JedisPool getJedisPool() {
		return jedisPool;
	}

	public void setJedisPool(JedisPool jedisPool) {
		this.jedisPool = jedisPool;
	}	

	
	public int getExpire() {
		return expire;
	}

	public void setExpire(int expire) {
		this.expire = expire;
	}

	@Override
	public void put(String sessionId, WxSessionMap sessionMap, int timeout){
		Jedis jedis = jedisPool.getResource();
		String preKey = this.keyPrefix + sessionId;
		try {
			jedis.set(preKey.getBytes(), SerializeUtils.serialize(sessionMap));
			jedis.expire(preKey, expire);
		} catch (Exception e) {
			log.error("Put session to redis error", e);
		} finally {
			jedisPool.returnResource(jedis);
		}
	}

	@Override
	public WxSessionMap get(String sessionId) {
		String preKey = this.keyPrefix + sessionId;
		Jedis jedis = jedisPool.getResource();
		WxSessionMap session = null;
		byte[] reslut = null;
		try {
			if (jedis.exists(preKey)) {
				reslut = jedis.get(preKey.getBytes());
				session = (WxSessionMap) SerializeUtils.deserialize(reslut);
			}
		} catch (Exception e) {
			log.error("Read session from redis error", e);
		} finally {
			jedisPool.returnResource(jedis);
		}
		return session;
	}

	@Override
	public void setMaxInactiveInterval(String sessionId, int interval) {
		String preKey = this.keyPrefix + sessionId;
		Jedis jedis = jedisPool.getResource();
		try {
			if (jedis.exists(preKey)) {
				jedis.expire(preKey, expire);
			}
		} catch (Exception e) {
			log.error("Set session max inactive interval to redis error", e);
		} finally {
			jedisPool.returnResource(jedis);
		}
	}

	@Override
	public void destroy(String sessionId) {
		String preKey = this.keyPrefix + sessionId;
		Jedis jedis = jedisPool.getResource();
		try {			
			if (jedis.exists(preKey)) {		
				log.debug("jedis.del(),key = " + preKey);
				jedis.del(preKey.getBytes());
			}
		} catch (Exception e) {
			log.error("Destroy session from redis error", e);
		} finally {
			jedisPool.returnResource(jedis);
		}

	}

	@Override
	public void putTicket(String sessionId, String casTicket, int timeout) {
		Jedis jedis = jedisPool.getResource();
		String preKey = this.ticketKeyPrefix + sessionId;
		try {
			jedis.set(preKey.getBytes(), SerializeUtils.serialize(casTicket));
			jedis.expire(preKey, expire);
		} catch (Exception e) {
			log.error("Put session to redis error", e);
		} finally {
			jedisPool.returnResource(jedis);
		}	
	}
	
	@Override
	public String destroyTicket(String casTicket) {
		String sessionId=null;
		String preKey = this.ticketKeyPrefix + casTicket;
		Jedis jedis = jedisPool.getResource();
		
		byte[] reslut = null;
		try {			
			if (jedis.exists(preKey)) {		
				reslut = jedis.get(preKey.getBytes());				
				sessionId =  (String) SerializeUtils.deserialize(reslut);
				log.debug("jedis.del(),key = " + preKey);
				jedis.del(preKey.getBytes());
			}
		} catch (Exception e) {
			log.error("Destroy session from redis error", e);
		} finally {
			jedisPool.returnResource(jedis);
		}
		return sessionId;
	}

}
